﻿using Assets.Script.gamecore.unit;
namespace Assets.Script.gamecore.action
{
    /// <summary>
    /// 移动动作
    /// </summary>
    public class MoveTo :  BaseAction
    {
        //起点到终点距离
        FixVector3 _moveDistance = new FixVector3(Fix64.Zero,Fix64.Zero,Fix64.Zero);
        //移动总共花费时间
        Fix64 _FixedMoveTime = Fix64.Zero;
        //经过时间
        Fix64 _moveElapseTime = Fix64.Zero;
        //移动开始位置
        FixVector3 _moveStartPosition = new FixVector3(Fix64.Zero, Fix64.Zero, Fix64.Zero);
        //移动结束位置
        FixVector3 _moveEndPosition = new FixVector3(Fix64.Zero, Fix64.Zero, Fix64.Zero);

        public override void UpdateLogic()
        {
            bool isOver = false;
            _moveElapseTime += GameConfig.g_fixFrameLen;

            Fix64 timeScale = _moveElapseTime / _FixedMoveTime;
            if(timeScale >= (Fix64)1)
            {
                timeScale = (Fix64)1;
            }

            FixVector3 elpaseDistance = new FixVector3(
                _moveDistance.x * timeScale,
                _moveDistance.y*timeScale,
                _moveDistance.z*timeScale);
            FixVector3 newPosition = new FixVector3(
                _moveStartPosition.x + elpaseDistance.x,
                _moveStartPosition.y + elpaseDistance.y,
                _moveStartPosition.z + elpaseDistance.z
                );
            //设置物体位置
            ControlObject.SetLogicPosition(newPosition);
            if(isOver)
            {
                //从manager中移除
                BelongManager.RemoveAction(this);
            }

        }

        public MoveTo(BaseObject obj,FixVector3 startPos,FixVector3 endPos,Fix64 time)
        {
            _moveStartPosition = startPos;
            _moveEndPosition = endPos;
            _FixedMoveTime = time;
            ControlObject = obj;
            if (_FixedMoveTime == Fix64.Zero)
            {
                _FixedMoveTime = (Fix64)0.1f;
            }

            _moveDistance = new FixVector3(endPos.x - startPos.x, endPos.y - endPos.y, endPos.z - endPos.z);
        }
    }
}

